//
//  fetchPhotoExhibitionList.m
//  PhotoCircle
//
//  Created by wujungao on 12/15/15.
//  Copyright © 2015 wujungao. All rights reserved.
//

#import "fetchData.h"
#import "photoExhibitionModel.h"
#import "bannerModel.h"
#import "GMBannerModel.h"
#import "GMHTTPSessionManager.h"
#import "GMString.h"
#import "PCCommentModel.h"
#import "photoExhibitionDetailModel.h"
#import "availableGamesModel.h"
#import "gameListModel.h"
#import "gameDetailModel.h"

static NSString *API_PhotoExhibitionIndex_Path=@"/api/v1/exhibition/index.json";
static NSString *API_PhotoExhibitionDetail_Path=@"/api/v1/exhibition/show.json";
static NSString *API_PhotoMatchIndex_Path=@"/api/v1/game/index.json";
static NSString *API_PhotoMatchDetail_Path=@"/api/v1/game/show.json";
static NSString *API_PhotoComment_Patch = @"/api/v1/comment/index.json";
static NSString *API_PhotoSubmitComment_Patch = @"/api/v1/comment/comment_create.json";
static NSString *API_PhotoLike_Patch = @"/api/v1/like/like.json";
static NSString *API_GameList_Path=@"/api/v1/game/available_games.json";
static NSString *API_Vote_Path=@"/api/v1/game/vote.json";

// 相册详情
static NSString *API_AlbumDetai_Path = @"/api/v1/my/album/photos.json";

@interface fetchData()

@property(strong,nonatomic)NSMutableArray *bannerModelArray;
@property(strong,nonatomic)NSMutableArray *photoExhibitionModelArray;
@property(strong,nonatomic)NSURLSessionDataTask *dataTask;
@property(strong,nonatomic)photoExhibitionDetailModel *pEDetailModel;
@property(strong,nonatomic)NSMutableArray *availabelGamesModelArray;
@property(strong,nonatomic)NSMutableArray *gameListModelArray;

@end

@implementation fetchData

#pragma mark - shared method
+ (instancetype)shared{
    
    static fetchData *fetch = nil;
    static dispatch_once_t once = 0;
    dispatch_once(&once, ^{
        
        fetch = [[fetchData alloc] init];
    });
    
    return fetch;
}

#pragma mark - fetch data method
-(void)fetchUserAlbumDetailWith:(NSDictionary*)pDic SuccessBlock:(successGameListBlock)sblock failBlock:(failBlock)fblock
{
    NSString *urlString=SERVER_URL(API_AlbumDetai_Path);
    [[GMHTTPSessionManager shared] GET:urlString parameters:pDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if([responseObject[@"code"] integerValue] == API_RESPONSE_OK){
            NSArray *photos = responseObject[@"data"];
            if(photos.count==0){
                
                return ;
            }
            
            NSMutableArray *albumDetais = [[NSMutableArray alloc] init];
            NSMutableArray *albumArray  = [[NSMutableArray alloc] init];
            for (NSDictionary *dict  in photos) {
                photoExhibitionModel *pM=[[photoExhibitionModel alloc] initPhotoExhibitionModelWithDict:dict];
                [albumDetais addObject:pM];
                [albumArray addObject:pM.imageURL];
            }
            sblock(albumArray);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fblock();
    }];
}


-(void)fetchExhibitionListWithParameterDict:(NSDictionary *)paraDict successBlock:(successExhibitionListBlock)sblock failBlock:(failBlock)fBlock{
    //影展列表
    [self.dataTask cancel];
    
    NSString *urlString=SERVER_URL(API_PhotoExhibitionIndex_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] GET:urlString parameters:paraDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if([responseObject[@"code"] integerValue] == API_RESPONSE_OK){
            
            NSDictionary *data=responseObject[@"data"];
            
            if(data.count==2){
                //请求的是第一页
                [self.bannerModelArray removeAllObjects];
                [self.photoExhibitionModelArray removeAllObjects];
                
                for(NSDictionary *dict in data[@"games"]){
                    
//                    bannerModel *bM=[[bannerModel alloc] initBannerModelWithDict:dict];
//                    [self.bannerModelArray addObject:bM];
                    GMBannerModel *gbM=[[GMBannerModel alloc] initModelWithDict:dict];
                    [self.bannerModelArray addObject:gbM];
                }
                
                for(NSDictionary *dict in data[@"exhibitions"]){
                    
                    photoExhibitionModel *pM=[[photoExhibitionModel alloc] initPhotoExhibitionModelWithDict:dict];
                    
                    [self.photoExhibitionModelArray addObject:pM];
                }
                
            }else if(data.count==1){
                //请求更多影展，>=第二页...
                for(NSDictionary *dict in data[@"exhibitions"]){
                    
                    photoExhibitionModel *pM=[[photoExhibitionModel alloc] initPhotoExhibitionModelWithDict:dict];
                    
                    [self.photoExhibitionModelArray addObject:pM];
                }
            }
            
            sblock(self.bannerModelArray,self.photoExhibitionModelArray);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        
        fBlock();
    }];
}

-(void)fetchExhibitionDetailWithParameterUUID:(NSString *)uuid successBlock:(successExhibtionDetailBlock)sblock failBlock:(failBlock)fBlock{
    //影展详情页面信息
        
    [self.dataTask cancel];
    
    NSDictionary *pDict=@{@"uuid":uuid};
    NSString *urlString=SERVER_URL(API_PhotoExhibitionDetail_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] GET:urlString parameters:pDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if([responseObject[@"code"] integerValue]==API_RESPONSE_OK){
            
            NSDictionary *data=responseObject[@"data"];
            if(data.count==0){
                
                return ;
            }
            
            self.pEDetailModel=[[photoExhibitionDetailModel alloc] initPhotoExhibitionDetailModelWithDict:data];
            
        }
        
        sblock(self.pEDetailModel);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        
        fBlock();
    }];
}

-(void)fetchMatchListWithParameterDict:(NSDictionary *)dict successBlock:(successMatchListBlock)sblock failBlock:(failBlock)fBlock{

    [self.dataTask cancel];

    NSString *urlString=SERVER_URL(API_PhotoMatchIndex_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] GET:urlString parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if([responseObject[@"code"] integerValue]==API_RESPONSE_OK){
            
            [self.gameListModelArray removeAllObjects];
            
            NSDictionary *data=responseObject[@"data"];
            
            for(NSDictionary *dict in data){
                
                gameListModel *gM=[[gameListModel alloc] initGameListModelWithDict:dict];
                
                [self.gameListModelArray addObject:gM];
            }
        }
        
        sblock(self.gameListModelArray);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        fBlock();
    }];
}

-(void)fetchMatchDetailWithParameterDict:(NSDictionary *)dict successBlock:(successMatchDetailBlock)sblock failBlock:(failBlock)fBlock{
        
    [self.dataTask cancel];
    NSString *urlString=SERVER_URL(API_PhotoMatchDetail_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] GET:urlString parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSInteger responCode = [responseObject[@"code"] integerValue];
        
        if (responCode == API_RESPONSE_OK) {
            
            NSDictionary *data=responseObject[@"data"];
            
            if (data.count!=0) {
                gameDetailModel *gdM=[[gameDetailModel alloc] initGameDetailModelWithDict:responseObject[@"data"]];
                
                sblock(gdM);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        fBlock();
    }];
}

-(void)getCommentDataWithPatameterDict:(NSDictionary*)dic sucessBlock:(sucessCommentBlock)commentBlock otherCode:(failCodeBlock)codeBlock failBlock:(failBlock)failError{
    
    NSString *urlStr = SERVER_URL(API_PhotoComment_Patch);
    
    self.dataTask = [[GMHTTPSessionManager shared] GET:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (![responseObject isKindOfClass:[NSDictionary class]]) {
            return ;
        }
        NSDictionary *responseDic = responseObject;
        NSInteger responseCode=  [responseDic[@"code"] integerValue];
        NSArray *dataArr = responseDic[@"data"];
        NSMutableArray *dataMuArr = [[NSMutableArray alloc]initWithCapacity:0];
        if (responseCode == 10000) {
            for (NSDictionary *childDic in dataArr) {
                PCCommentModel *model = [[PCCommentModel alloc]initWithDictionary:childDic];
                [dataMuArr addObject:model];
            }
            
            commentBlock(dataMuArr);
        }else{
            codeBlock(responseCode);
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failError();
     } cachePolicy:NSURLRequestUseProtocolCachePolicy ];
    
}

-(void)postSubmitCommentDataWithPatameterDic:(NSDictionary*)pDic imageData:(NSData*)imgData sucessBlock:(sucessSubmitCommentBlock)submitBlock otherCodeBlock:(failCodeBlock)codeBlock  failWithBlock:(failBlock)failError;
{
     NSString *urlStr = SERVER_URL(API_PhotoSubmitComment_Patch);
    
    self.dataTask = [[GMHTTPSessionManager shared] POST:urlStr parameters:pDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (imgData) {
            [formData appendPartWithFileData:imgData name:@"image" fileName:@"comment.jpg" mimeType:@"image/jpg"];
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *responseDic = responseObject;
        NSInteger responseCode=  [responseDic[@"code"] integerValue];
        codeBlock(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failError();
    }];    
}
/* 
 {
 "code": 10000,
 "data": 
      {
      "status": true
      }
 }
 */
-(void)postSupportDic:(NSDictionary *)pDic sucessBlock:(sucessSupportBlock)sucessBlock otherCodeBlock:(failCodeBlock)codeBlock failWithBlock:(failBlock)failError{
    
    NSString *urlStr = SERVER_URL(API_PhotoLike_Patch);
    self.dataTask = [[GMHTTPSessionManager shared] POST:urlStr parameters:pDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if (![responseObject isKindOfClass:[NSDictionary class]]) {
            return ;
        }
        NSInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == 10000) {
            sucessBlock();
        }else{
            codeBlock(responseCode);
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failError();
    }];
    
}

-(void)fetchGameListSuccessBlock:(successGameListBlock)sblock failBlock:(failBlock)fblock{
    
    [self.dataTask cancel];
    NSString *urlString=SERVER_URL(API_GameList_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if([responseObject[@"code"] integerValue]==API_RESPONSE_OK){
            
            [self.availabelGamesModelArray removeAllObjects];
            
            for(NSDictionary *dict in responseObject[@"data"]){
                
                availableGamesModel *agM=[[availableGamesModel alloc] initAvailableGamesModelWithDict:dict];
                
                [self.availabelGamesModelArray addObject:agM];
            }
        }
        
        sblock(self.availabelGamesModelArray);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

-(void)postVoteWithParameterDict:(NSDictionary *)dict successBlock:(successVoteBlock)sblock failBlock:(failBlock)fblock{
    
    [self.dataTask cancel];
    NSString *urlString=SERVER_URL(API_Vote_Path);
    
    self.dataTask=[[GMHTTPSessionManager shared] POST:urlString parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSInteger voteStateCode=[responseObject[@"code"] integerValue];
        sblock(voteStateCode);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        fblock();
    }];
}

#pragma mark - property
-(NSMutableArray *)bannerModelArray{
    
    if(_bannerModelArray==nil){
        
        _bannerModelArray=[[NSMutableArray alloc] initWithCapacity:8];
    }
    
    return _bannerModelArray;
}

-(NSMutableArray *)photoExhibitionModelArray{
    
    if(_photoExhibitionModelArray==nil){
        
        _photoExhibitionModelArray=[[NSMutableArray alloc] initWithCapacity:8];
    }
    
    return _photoExhibitionModelArray;
}

-(NSMutableArray *)availabelGamesModelArray{
    
    if(_availabelGamesModelArray==nil){
        
        _availabelGamesModelArray=[[NSMutableArray alloc] initWithCapacity:8];
    }
    
    return _availabelGamesModelArray;
}

-(NSMutableArray *)gameListModelArray{
    
    if(_gameListModelArray==nil){
        
        _gameListModelArray=[[NSMutableArray alloc] initWithCapacity:8];
    }
    
    return _gameListModelArray;
}

#pragma mark - private

@end
